layui.use('upload', function () {
    var $ = layui.jquery
        , upload = layui.upload;


//多文件列表示例
    var demoListView = $('#demoList')
        , uploadListIns = upload.render({
        elem: '#testList'
        , url: 'uploadOnlyPic' //改成您自己的上传接口
        , accept: 'images'
        , multiple: true
        , auto: false
        , size: 800
        , bindAction: '#testListAction'
        , choose: function (obj) {
            var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
            //读取本地文件
            obj.preview(function (index, file, result) {
                var tr = $(['<tr id="upload-' + index + '">'
                    , '<td>' + file.name + '</td>'
                    , '<td>' + (file.size / 1024).toFixed(1) + 'kb</td>'
                    , '<td>等待上传</td>'
                    , '<td>'
                    , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                    , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                    , '</td>'
                    , '</tr>'].join(''));

                //单个重传
                tr.find('.demo-reload').on('click', function () {
                    obj.upload(index, file);
                });

                //删除
                tr.find('.demo-delete').on('click', function () {
                    delete files[index]; //删除对应的文件
                    tr.remove();
                    uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                });

                demoListView.append(tr);
                $(".layui-upload-list").removeClass('dd');
            });
        }
        , done: function (res, index, upload) {
            // if (res.files.file) { //上传成功原来是这个
            if (res.code == 0) { //上传成功
                var tr = demoListView.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                tds.eq(3).html(''); //清空操作
                $(".layui-laypage-btn").trigger("click");
                $(".layui-upload-list").addClass('dd');


                return delete this.files[index]; //删除文件队列已经上传成功的文件
            }
            this.error(index, upload);

        }
        , error: function (index, upload) {
            var tr = demoListView.find('tr#upload-' + index)
                , tds = tr.children();
            tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
            tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
        }
    });
});

layui.use(['table', 'layer'], function () {
    // var table = layui.table;
    var layer = layui.layer;
    var table = layui.table
        , form = layui.form
        , $ = layui.$;


    table.render({
        elem: '#test'
        , url: '/picturelist'
        , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板

        //filter,exports,print
        , defaultToolbar: ['', '', ''
        ]
        , title: '用户数据表'
        , cols: [[
            {type: 'checkbox', fixed: 'left',  style: 'height:150px'}
            , {type:'numbers', title: '序号', width: 60, fixed: 'left', style: 'height:150px'}
            // , {field: 'name', title: '姓名', width: 90}不显示主键id
            , {
                field: 'url',
                title: '照片',
                templet: '<div><img  src="{{d.url}}"  style="height: 138px"/></div>',
                style: 'height: 150px'
            }
            //考虑改成姓名
            // , {field: 'studentId', title: '学生', width: 90}
            , {field: 'createTime', title: '创建时间', width: 150}
            // , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 180}
        ]]
        , page: true
        // , id: 'test'
        , height: 'full'
        , limits: [1, 2, 3, 4, 5, 10]
        , limit: 5
        , id: 'test'

    });
    table.on('toolbar(test)', function (obj) {

        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'getCheckData':
                var data = checkStatus.data;
                console.log(JSON.stringify(data))
                console.log(data);
                $.ajax({
                    url: '/deletepic',
                    type: 'post',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    contentType: 'application/json;charset=UTF-8',
                    success: function (data) {
                        if (data.code == 0) {
                            $(".layui-laypage-btn").trigger("click");
                        }
                    },
                    error: function (error) {
                        console.log(error)
                    }
                });
                // layer.alert(JSON.stringify(data));
                break;
            case 'getCheckLength':

                break;
            case 'isAll':


                break;
            //自定义头工具栏右侧图标 - 提示
            case 'LAYTABLE_TIPS':
                layer.alert('这是工具栏右侧自定义的一个图标按钮');
                break;
        }

    });


    //监听行工具事件
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {

        } else if (obj.event === 'edit') {
            console.log(obj.data);
            layer.open({
                type: 1,
                title: '选择',
                maxmin: true,
                shadeClose: true, //点击遮罩关w闭层
                area: ['350px', '350px'],
                content: $("#choose"),
                cancel: function () {
                    $(".layui-laypage-btn").trigger("click");
                    //return false 开启该代码可禁止点击该按钮关闭
                },
                end: function () {
                    //z在弹出层的按钮提交后
                    // 成功执行 var index = parent.layer.getFrameIndex(window.name);
                    //                         setTimeout(function(){parent.layer.close(index)}, 700);
                    $(".layui-laypage-btn").trigger("click");
                }
            });

        } else if (obj.event === 'up') {

        } else if (obj.event === 'delpic') {
        }
    });

});

